package com.feonix.common.util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.log4j.Appender;
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

public class LogUtil {
	private static String[] levelNames = { "none", "error", "warn", "info", "debug" };

	/**
	 * 通过日志级别名称获得对应 id
	 * 
	 * @param loglevel
	 * @return
	 */
	public static Integer getLogLevel(String loglevel) {
		return Integer.valueOf(ArrayUtils.indexOf(levelNames, loglevel));
	}

	/**
	 * 通过日志级别id 获得 相应名称
	 * 
	 * @param logLevel
	 * @param type
	 *            类型1:主日志，2:sql日志
	 * @return
	 */
	public static String getLogLevelName(Integer logLevel, Integer type) {
		return levelNames[logLevel.intValue()];

	}

	/**
	 * 通过日志级别id 获得 相应名称
	 * 
	 * @param logLevel
	 * @param type
	 *            类型1:主日志，2:sql日志
	 * @param string
	 * @param properties
	 * @return
	 */
	public static String getLogLevelNameValue(Integer logLevel, Integer type, Properties properties,
			final String propertyKey) {
		String value = properties.getString(propertyKey);
		if (StringUtil.isEmpty(value)) {
			return levelNames[logLevel.intValue()];
		}
		String[] values = StringUtil.split(value, ",");
		List<String> tmpValues = new LinkedList<String>();
		tmpValues.add(levelNames[logLevel.intValue()]);
		for (String tmpValue : values) {
			if (ArrayUtils.contains(levelNames, tmpValue)) {
				continue;
			}
			tmpValues.add(tmpValue);
		}
		return StringUtil.join(tmpValues, ",");
	}

	/**
	 * 更改log4j日志
	 * 
	 * @param loglevel
	 */
	public static void changeLogLevel(Integer loglevel) {
		int i = loglevel.intValue();
		switch (i) {
		case 1:
			Logger.getLogger("com.feonix").setLevel(Level.ERROR);
			Logger.getLogger("jdbc.sqlonly").setLevel(Level.ERROR);
			break;
		case 2:
			Logger.getLogger("com.feonix").setLevel(Level.WARN);
			Logger.getLogger("jdbc.sqlonly").setLevel(Level.ERROR);
			break;
		case 3:
			Logger.getLogger("com.feonix").setLevel(Level.INFO);
			Logger.getLogger("jdbc.sqlonly").setLevel(Level.INFO);
			break;
		case 4:
			Logger.getLogger("com.feonix").setLevel(Level.DEBUG);
			Logger.getLogger("jdbc.sqlonly").setLevel(Level.INFO);
			break;
		default:
			Logger.getLogger("com.feonix").setLevel(Level.ERROR);
			Logger.getLogger("jdbc.sqlonly").setLevel(Level.ERROR);
		}
	}

	/**
	 * 创建一个每天生成的日志
	 * 
	 * @param name
	 *            日志名称 - 英文
	 * @param path
	 *            日志路径，带文件名称
	 * @param appName
	 *            应用名称
	 * @return
	 */
	public static Appender createDailyFileLog(String name, String path, String appName) {
		DailyRollingFileAppender rollingFileAppender = new DailyRollingFileAppender();
		PatternLayout patternLayout = new PatternLayout();
		patternLayout.setConversionPattern(
				"%d{yyyy-MM-dd HH:mm:ss} [" + appName + "] %5p %c{1}:%L - %m (feonix Co. Ltd.)%n");
		rollingFileAppender.setName(name);
		rollingFileAppender.setFile(path);
		rollingFileAppender.setDatePattern("'_'yyyy-MM-dd'.log'");
		rollingFileAppender.setLayout(patternLayout);
		rollingFileAppender.activateOptions();
		return rollingFileAppender;
	}

	/**
	 * 设置日志打印
	 * 
	 * @param clazz
	 * @param level
	 * @param appender
	 * @param additive
	 *            是否继承上层
	 */
	public static void setLogLevel(String clazz, String level, Appender appender, boolean additive) {
		Logger logger = Logger.getLogger(clazz);
		logger.removeAllAppenders();
		logger.addAppender(appender);
		logger.setAdditivity(additive);
		logger.setLevel(Level.toLevel(level, Level.OFF));
	}
}
